package cn.znzz.cjm.org.service.impl;

import cn.znzz.cjm.basic.service.impl.BaseServiceImpl;
import cn.znzz.cjm.basic.util.PageList;
import cn.znzz.cjm.org.domain.Department;
import cn.znzz.cjm.org.mapper.DepartmentMapper;
import cn.znzz.cjm.org.query.DepartmentQuery;
import cn.znzz.cjm.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 部门业务层实现
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    //基本的crud被抽取

    //业务不满足，就复写
    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    public void update(Department department) {
        //1.顶级
        String dirPath = "";
        if(department.getParent()==null){
            dirPath = "/"+department.getId();
        }else {//2.非顶级
            Department parent = departmentMapper.loadById(department.getParent().getId());
            dirPath = parent.getDirPath()+"/"+department.getId();

        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public List<Department> queryTree() {
        return departmentMapper.queryTree();
    }
}
